1
|
|
|
import { Inject } from '@nestjs/common'; |
2
|
|
|
import { CommandHandler } from '@nestjs/cqrs'; |
3
|
|
|
import { |
4
|
|
|
Notification, |
5
|
|
|
NotificationType |
6
|
|
|
} from 'src/Domain/Notification/Notification.entity'; |
7
|
|
|
import { CreateNotificationCommand } from './CreateNotificationCommand'; |
8
|
|
|
import { INotificationRepository } from 'src/Domain/Notification/Repository/INotificationRepository'; |
9
|
|
|
import { IMattermostNotifier } from 'src/Application/IMattermostNotifier'; |
10
|
|
|
import { ConfigService } from '@nestjs/config'; |
11
|
|
|
|
12
|
|
|
const { MATTERMOST_CHANNEL_LEAVES_ID } = process.env; |
13
|
|
|
|
14
|
|
|
@CommandHandler(CreateNotificationCommand) |
15
|
|
|
export class CreateNotificationCommandHandler { |
16
|
|
|
constructor( |
17
|
|
|
@Inject('INotificationRepository') |
18
|
|
|
private readonly notificationRepository: INotificationRepository, |
19
|
|
|
@Inject('IMattermostNotifier') |
20
|
|
|
private readonly mattermostNotifier: IMattermostNotifier, |
21
|
|
|
private readonly configService: ConfigService |
22
|
|
|
) {} |
23
|
|
|
|
24
|
|
|
public async execute(command: CreateNotificationCommand): Promise<string> { |
25
|
|
|
const { message, type, leaveReaquest } = command; |
26
|
|
|
|
27
|
|
|
if (type === NotificationType.POST) { |
28
|
|
|
const { id } = await this.mattermostNotifier.createPost( |
29
|
|
|
MATTERMOST_CHANNEL_LEAVES_ID, |
30
|
|
|
message |
31
|
|
|
); |
32
|
|
|
const notification = await this.notificationRepository.save( |
33
|
|
|
new Notification(type, message, id, leaveReaquest) |
34
|
|
|
); |
35
|
|
|
|
36
|
|
|
return notification.getId(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
if (type === NotificationType.REACTION) { |
40
|
|
|
const rootNotification = await this.getRootNotification( |
41
|
|
|
leaveReaquest.getId() |
42
|
|
|
); |
43
|
|
|
|
44
|
|
|
await this.mattermostNotifier.createReaction( |
45
|
|
|
rootNotification.getResourceId(), |
46
|
|
|
message |
47
|
|
|
); |
48
|
|
|
|
49
|
|
|
const notification = await this.notificationRepository.save( |
50
|
|
|
new Notification( |
51
|
|
|
type, |
52
|
|
|
message, |
53
|
|
|
rootNotification.getResourceId(), |
54
|
|
|
leaveReaquest |
55
|
|
|
) |
56
|
|
|
); |
57
|
|
|
|
58
|
|
|
return notification.getId(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if (type === NotificationType.COMMENT) { |
62
|
|
|
const rootNotification = await this.getRootNotification( |
63
|
|
|
leaveReaquest.getId() |
64
|
|
|
); |
65
|
|
|
|
66
|
|
|
const { id } = await this.mattermostNotifier.createComment( |
67
|
|
|
this.configService.get<string>('MATTERMOST_CHANNEL_LEAVES_ID'), |
68
|
|
|
message, |
69
|
|
|
rootNotification.getResourceId() |
70
|
|
|
); |
71
|
|
|
|
72
|
|
|
const notification = await this.notificationRepository.save( |
73
|
|
|
new Notification(type, message, id, leaveReaquest) |
74
|
|
|
); |
75
|
|
|
|
76
|
|
|
return notification.getId(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
throw new Error('Type not managed'); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
private async getRootNotification(leaveReaquestId: string) { |
83
|
|
|
return await this.notificationRepository.findByLeaveRequestIdAndType( |
84
|
|
|
leaveReaquestId, |
85
|
|
|
NotificationType.POST |
86
|
|
|
); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|